package com.xzll.test.config;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MergeStrategy implements CellWriteHandler {

	@Override
	public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
								 Row row, Head head, Integer columnIndex, Integer relativeRowIndex,
								 Boolean isHead) {

	}

	@Override
	public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
								Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

	}

	@Override
	public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

	}

	@Override
	public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
								 List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex,
								 Boolean isHead) {
		int rowIndex = cell.getRowIndex();
		int columnIndex = cell.getColumnIndex();
		//自己指定需要合并的单元格区域
		List<Integer> collect = Stream.of(0, 1, 2, 4, 5, 6, 7).collect(Collectors.toList());
		if (rowIndex % 2 == 1 ){//&& collect.contains(columnIndex)
			CellRangeAddress cellRangeAddress = new CellRangeAddress(rowIndex, rowIndex+1, columnIndex,
					columnIndex + 1);
			writeSheetHolder.getSheet().addMergedRegionUnsafe(cellRangeAddress);
		}
	}

}
//
//	// 合并的列编号，从0开始，指定的index或自己按字段顺序数
//	private Set<Integer> mergeCellIndex = new HashSet<>();
//
//	// 数据集大小，用于区别结束行位置
//	private Integer maxRow = 0;
//
//	// 禁止无参声明
//	private MergeStrategy() {
//	}
//
//	public MergeStrategy(Integer maxRow, int... mergeCellIndex) {
//		Arrays.stream(mergeCellIndex).forEach(item -> {
//			this.mergeCellIndex.add(item);
//		});
//		this.maxRow = maxRow;
//	}
//
//	// 记录上一次合并的信息
//	private Map<Integer, MergeRange> lastRow = new HashedMap();
//
//	// 每行每列都会进入，绝对不要在这写循环
//	@Override
//	protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
//		int currentCellIndex = cell.getColumnIndex();
//		// 判断该列是否需要合并
//		if (mergeCellIndex.contains(currentCellIndex)) {
//			String currentCellValue = cell.getStringCellValue();
//			int currentRowIndex = cell.getRowIndex();
//			if (!lastRow.containsKey(currentCellIndex)) {
//				// 记录首行起始位置
//				lastRow.put(currentCellIndex, new MergeRange(currentCellValue, currentRowIndex, currentRowIndex, currentCellIndex, currentCellIndex));
//				return;
//			}
//			//有上行这列的值了，拿来对比.
//			MergeRange mergeRange = lastRow.get(currentCellIndex);
//			if (!(mergeRange.lastValue != null && mergeRange.lastValue.equals(currentCellValue))) {
//				// 结束的位置触发下合并.
//				// 同行同列不能合并，会抛异常
//				if (mergeRange.startRow != mergeRange.endRow || mergeRange.startCell != mergeRange.endCell) {
//					sheet.addMergedRegionUnsafe(new CellRangeAddress(mergeRange.startRow, mergeRange.endRow, mergeRange.startCell, mergeRange.endCell));
//				}
//				// 更新当前列起始位置
//				lastRow.put(currentCellIndex, new MergeRange(currentCellValue, currentRowIndex, currentRowIndex, currentCellIndex, currentCellIndex));
//			}
//			// 合并行 + 1
//			mergeRange.endRow += 1;
//			// 结束的位置触发下最后一次没完成的合并
//			if (relativeRowIndex.equals(maxRow - 1)) {
//				MergeRange lastMergeRange = lastRow.get(currentCellIndex);
//				// 同行同列不能合并，会抛异常
//				if (lastMergeRange.startRow != lastMergeRange.endRow || lastMergeRange.startCell != lastMergeRange.endCell) {
//					sheet.addMergedRegionUnsafe(new CellRangeAddress(lastMergeRange.startRow, lastMergeRange.endRow, lastMergeRange.startCell, lastMergeRange.endCell));
//				}
//			}
//		}
//	}
//}
//
//class MergeRange {
//	public int startRow;
//	public int endRow;
//	public int startCell;
//	public int endCell;
//	public String lastValue;
//
//	public MergeRange(String lastValue, int startRow, int endRow, int startCell, int endCell) {
//		this.startRow = startRow;
//		this.endRow = endRow;
//		this.startCell = startCell;
//		this.endCell = endCell;
//		this.lastValue = lastValue;
//	}


